﻿<%@ WebHandler Language="C#" Class="lists" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using System.IO;

public class lists : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        //string query = HttpContext.Current.Request.Params["page"]


        //if (string.IsNullOrEmpty(query))
        //{
        //    HttpContext.Current.Response.Write("parameter error");
        //    return;
        //}

        var myurl= "https://js.50cms.com/" +SaveFile();

        var code = 1;
        var msg = "获取成功";
        var time = DateTime.Now;
        var data = new { full_url =myurl, url =myurl };


        context.Response.Write(JsonConvert.SerializeObject(new { code,msg,time,data}));



        HttpContext.Current.Response.End();
        //context.Response.Write("Hello World");


    }

    private string SaveFile(string basePath = "/Upload/")
    {

        string _datePath = DateTime.Now.ToString("yyyyMMdd") + "/";
        basePath += _datePath;

        string serverPath = "~"+basePath;
        var name = string.Empty;
        serverPath = (serverPath.IndexOf("~") > -1) ? System.Web.HttpContext.Current.Server.MapPath(serverPath) :
        serverPath;
        HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

        if (!Directory.Exists(serverPath))
            Directory.CreateDirectory(serverPath);

        var suffix = files[0].ContentType.Split('/');
        var _suffix = suffix[1];
        var _temp = System.Web.HttpContext.Current.Request["name"];

        //if (!string.IsNullOrEmpty(_temp))
        //{
        //    name = _temp;
        //}
        //else
        //{
        Random rand = new Random(24 * (int)DateTime.Now.Ticks);
        name = rand.Next() + ".jpg";
        //}

        var full = serverPath + name;
        files[0].SaveAs(full);
        return basePath + name;

    }

    public bool IsReusable {
        get {
            return false;
        }
    }




}